Shader "water"
{
Properties
{
	_Color ("Main Color", Color) = (0, 0.15, 0.115, 1)
	_MainTex ("Base (RGB)", 2D) = "white" {}
	_WaveMap ("Wave Map", 2D) = "bump" {}
	_Cubemap ("Environment Cubemap", Cube) = "_Skybox" {}
	_WaveXSpeed ("Wave Horizontal Speed", Range(-0.1, 0.1)) = 0.01
	_WaveYSpeed ("Wave Vertical Speed", Range(-0.1, 0.1)) = 0.01
	_ShallowColor("ShallowColor",Color) = (1,1,1,1)
	_DepthColor("DepthColor",Color) = (0,0,0,0)
	_RefrDistortion ("RefrDistortion", Range(0, 5000)) = 10
	_ReflDistortion ("ReflDistortion", Range(0, 20)) = 10

	_DepthStength ("DepthStength", Range(0,10)) = 10 //Range(50,0.1

	_Reflection  ("Reflection", 2D) = "white" {}  //镜面反射

	_Range ("Range", vector) = (0.13, 1.53, 0.37, 0.78)

	_FoamStrength ("Foam strength", Range (0, 10.0)) = 1.0
	[NoScaleOffset] _Foam ("Foam texture", 2D) = "white" {}
	[NoScaleOffset] _FoamGradient ("Foam gradient ", 2D) = "white" {}

	_WaveScale("WaveScale", Range(0,10)) = 0.1
	_WaveColor("WaveColor", Color) = (1,1,1,1) 

}
SubShader
{
	Tags { "Queue" = "Transparent" "RenderType"="Transparent"}

	GrabPass { "_RefractionTex" }

	zwrite off


	Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct v2f
{
	float4 vertex : SV_POSITION;
	float4 uv : TEXCOORD0;
	float4 scrPos : TEXCOORD1;  
	float4 TtoW0 : TEXCOORD2;  
	float4 TtoW1 : TEXCOORD3;  
	float4 TtoW2 : TEXCOORD4; 
	float2 foamUV : TEXCOORD5;
};

fixed4 _DepthColor;
fixed4 _ShallowColor;
float _DepthStength;

float4 _Range;

fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _WaveMap;
float4 _WaveMap_ST;

fixed _WaveXSpeed;
fixed _WaveYSpeed;
float _RefrDistortion;
float _ReflDistortion;	
sampler2D _RefractionTex;
float4 _RefractionTex_TexelSize;

samplerCUBE _Cubemap;
sampler2D _Reflection; // 反射贴图
float4 _Reflection_TexelSize;

uniform float _FoamStrength;
sampler2D _Foam;
sampler2D _FoamGradient;

sampler2D _WaveResult;
float _WaveScale;
fixed4 _WaveColor; 

sampler2D _CameraDepthTexture;

v2f vert (appdata_full v)
{
	v2f o;
	//o.vertex = UnityObjectToClipPos(v.vertex);
	
	//---------- 交互 ---------
	float4 localPos = v.vertex;
	float2 waveUv = v.texcoord.xy;
	// #if UNITY_UV_STARTS_AT_TOP
	// 	waveUv =  1.0 - waveUv;
	// #endif
	float4 waveTransmit = tex2Dlod(_WaveResult, float4(waveUv, 0, 0));
	float waveHeight = DecodeFloatRGBA(waveTransmit);

	localPos.y += waveHeight * _WaveScale;
	float3 worldPos = mul(unity_ObjectToWorld, localPos);

	o.vertex = mul(UNITY_MATRIX_VP, float4(worldPos, 1));


	o.uv.xy = v.texcoord.xy; //TRANSFORM_TEX(v.texcoord, _MainTex);
	o.uv.zw = TRANSFORM_TEX(v.texcoord, _WaveMap);
	o.scrPos = ComputeGrabScreenPos(o.vertex);
	//COMPUTE_EYEDEPTH(o.scrPos.z); 

	//float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;  
	fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);  
	fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);  
	fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w; 
	
	o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);  
	o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);  
	o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);  

	float temp = sin(_Time.x);
	//o.foamUV = 7.0f * worldPos.xz + 0.05 * float2(temp, temp);

	o.foamUV = 7.0f * worldPos.xz + 10* float2(temp, temp);




	return o;
}

fixed4 frag (v2f i) : SV_Target
{

	//--------- 深浅 ------------     
	//利用屏幕坐标采样深度图，并对深度解码得到深度图中的线性深度（摄像机空间）
	float depthTexDepth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos))); 

	float screenDepth = i.scrPos.z / i.scrPos.w; //经过齐次除法，得到屏幕深度值
	screenDepth = (UNITY_NEAR_CLIP_VALUE >= 0) ? screenDepth : screenDepth * 0.5 + 0.5; //根据平台不同，修正屏幕深度值
	screenDepth = LinearEyeDepth(screenDepth); //得到线性的屏幕深度
	half deltaDepth = depthTexDepth - screenDepth;//计算深度差
	float diff = 1-saturate((deltaDepth - 0.5 ) * _DepthStength);
	//float diff = 1-saturate(((depthTexDepth-i.scrPos.z) - 0.5 ) * _DepthStength);

	fixed4 depthCol = lerp(_DepthColor, _ShallowColor, diff);
	//return depthCol;


	//---------- 折射 ---------
	float2 speed = _Time.y * float2(_WaveXSpeed, _WaveYSpeed);
	
	// Get the normal in tangent space
	// fixed3 bump1 = UnpackNormal(tex2D(_WaveMap, i.uv.zw + speed)).rgb;
	// fixed3 bump2 = UnpackNormal(tex2D(_WaveMap, i.uv.zw - speed)).rgb;

	fixed3 bump1 = UnpackNormal(tex2D(_WaveMap, i.uv.zw )).rgb;
	fixed3 bump2 = UnpackNormal(tex2D(_WaveMap, float2(i.uv.w, 1.0 - i.uv.z))).rgb;

	fixed3 bump = normalize(bump1 + bump2) ;

	// Compute the offset in tangent space
	float2 offset = bump.xy * _RefrDistortion * _RefractionTex_TexelSize.xy;
	i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;
	fixed3 refrCol = tex2D( _RefractionTex, i.scrPos.xy/i.scrPos.w).rgb;// * fixed3(0.5,0.5,0.2);

	//return fixed4(refrCol, 1);

	//---------- 反射 ---------

	float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
	fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));


	// Convert the normal to world space
	float3 tangentNormal = fixed3(0,0,1);//bump;//
	//return fixed4(bump, 1);//看法线
	fixed3 worldNormal = normalize(half3(dot(i.TtoW0.xyz, tangentNormal), dot(i.TtoW1.xyz, tangentNormal), dot(i.TtoW2.xyz, tangentNormal)));
	//fixed3 worldNormal = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));

		//------ cubeMap
	// fixed4 texColor = tex2D(_MainTex, i.uv.xy + speed);
	// fixed3 reflDir = reflect(-viewDir, worldNormal);
	// fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb ;// * _Color.rgb;

		//------ 对称相机
	float2 screenUV = i.scrPos.xy/i.scrPos.w - worldNormal.xz * _ReflDistortion*_Reflection_TexelSize.xy;
	fixed4 texColor = tex2D(_MainTex, i.uv.xy + speed);
	fixed3 reflCol = tex2D(_Reflection, screenUV).rgb  * _Color.rgb;//* texColor.rgb

	//return fixed4(reflCol, 1);

	//---------- 菲涅尔效应 ---------
	//return fixed4(1,1,1,1) * pow(1 - saturate(dot(viewDir, worldNormal)), 4);

	fixed fresnel = pow(1 - saturate(dot(viewDir, worldNormal)), 4);// 水F0 ~= 0
	fixed3 reColor = reflCol * fresnel + refrCol * (1 - fresnel);

	//return fixed4(reColor, 1);

	//---------- 浪花 ---------
	float intensityFactor = 1 - saturate(deltaDepth / _FoamStrength);    
	half3 foamGradient = 1 - tex2D(_FoamGradient, float2(intensityFactor - _Time.y*0.2, 0) + bump.xy * 0.15);

	float2 foamDistortUV = bump.xy * 0.2;
	half3 foamTex = tex2D(_Foam, i.foamUV + foamDistortUV).rgb;
	fixed4 foamColor = fixed4(foamGradient * intensityFactor * foamTex, 1);

	//return foamColor + fixed4(reColor, 1) ;

	//---------- 交互 ---------
	float4 waveTransmit = tex2Dlod(_WaveResult, float4(i.uv.xy, 0, 0));
	float waveHeight = DecodeFloatRGBA(waveTransmit) * _WaveScale;
	
	//return lerp(fixed4(reColor, 1) * depthCol + foamColor, _WaveColor, waveHeight);
	return lerp(fixed4(0,0,0, 1) , _WaveColor, waveHeight) + foamColor + fixed4(reColor, 1);

	

	//---------- 交互+浪花 ---------
	//float intensityFactor =  (1 - saturate(deltaDepth / _FoamStrength)) * waveHeight * 20;    //
	// float intensityFactor =  (1 - saturate(deltaDepth / 100)) * waveHeight * 30;    //
	//half3 foamGradient = 1 - tex2D(_FoamGradient, float2(intensityFactor - _Time.y*0.2, 0) + bump.xy * 0.15);

	// float2 foamDistortUV = bump.xy * 0.2;
	// half3 foamTex = tex2D(_Foam, i.foamUV + foamDistortUV).rgb;
	// fixed4 foamColor = fixed4(intensityFactor  * foamTex , 1);//* foamGradient

	// return foamColor;


	//fixed4 finalColor = fixed4(reColor, 1) * depthCol + foamColor;
	//return finalColor;

	//=========================== 其它 =========================
	//---------- 透明 ---------
	//fixed Alpha = 0;
	// fixed Alpha = min(_Range.x, deltaDepth)/_Range.x;
	// return fixed4(0,0,0, Alpha);

	//==========================================================


	}
ENDCG
	}
}
    FallBack "Diffuse"
}
